`include "common_define.vh"
module ethercat_header_process(
    input epu_clk,
    input rst_n,
    input [3:0] state,
    input [2:0] cdc_counter,
    input [10:0] len_counter,
    input [23:0] shift_reg,
    input [15:0] configured_station_address_r,
    input [15:0] configured_station_alias_r,
    output reg [10:0] payload_len,
    output reg more_datagram,
    output reg [31:0] logical_start_address,
    output reg [1:0] operation,
    output reg [1:0] cmd_type,
    output reg circulating_frame
);
    //payload length register
    always @(posedge epu_clk or negedge rst_n) begin
        if(!rst_n) begin
            payload_len <= 11'b0;
        end
        else if(state==`DATAGRAM_HEADER && len_counter==8) begin
            payload_len <= shift_reg[10:0];
        end
        else if(state != `DATAGRAM_HEADER && state != `DATAGRAM_PAYLOAD) begin
            payload_len <= 11'b0;
        end
    end

    // more datagram register
    always @(posedge epu_clk or negedge rst_n) begin
        if(!rst_n) begin
            more_datagram <= 1'b0;
        end
        else if(state==`IDLE || state==`ERROR) begin
            more_datagram <= 1'b0;
        end
        else if(state==`DATAGRAM_HEADER && len_counter==8) begin
            more_datagram <= shift_reg[15];
        end
    end

    // logical start address register
    always @(posedge epu_clk or negedge rst_n) begin
        if(!rst_n) begin
            logical_start_address <= 32'b0;
        end
        else if(state==`DATAGRAM_HEADER && len_counter==4) begin
            logical_start_address[15:0] <= shift_reg[15:0];
        end
        else if(state!=`DATAGRAM_HEADER && len_counter==6) begin
            logical_start_address[31:16] <= shift_reg[15:0];
        end
        else if(state!=`DATAGRAM_HEADER && state!=`DATAGRAM_PAYLOAD) begin
            logical_start_address <= 32'b0;
        end 
    end

    // circulating frame
    always@(posedge epu_clk or negedge rst_n) begin
        if(!rst_n) begin
            circulating_frame <= 1'b0;
        end
        else if(state==`DATAGRAM_HEADER && cdc_counter==8) begin
            circulating_frame <= shift_reg[14];
        end
        else if(state!=`DATAGRAM_HEADER && state!=`ERROR) begin
            circulating_frame <= 1'b0;
        end
    end

    //ethercat command type
    reg [3:0] cmd;

    always @(posedge epu_clk or negedge rst_n) begin
        if(!rst_n) begin
            cmd <= 4'b0;
        end
        else if(state==`DATAGRAM_HEADER && len_counter==1) begin
            cmd <= shift_reg[15:8];
        end
        else if(state!=`DATAGRAM_HEADER && state!=`DATAGRAM_PAYLOAD) begin
            cmd <= 4'b0;
        end
    end

    always @(posedge epu_clk or negedge rst_n) begin
        if(!rst_n) begin
            operation <= `NONE;
            cmd_type <= `GENERAL;
        end
        else if(state==`DATAGRAM_HEADER && len_counter==4) begin
            case(cmd)
                `NOP: begin
                    operation <= `NONE;
                    cmd_type <= `GENERAL;
                end
                `APRD: begin
                    if(shift_reg[15:0]==0) begin
                        operation <= `READ;
                        cmd_type <= `GENERAL;
                    end
                end
                `APWR: begin
                    if(shift_reg[15:0]==0) begin
                        operation <= `WRITE;
                        cmd_type <= `GENERAL;
                    end
                end
                `APRW: begin
                    if(shift_reg[15:0]==0) begin
                        operation <= `WRITE_AFTER_READ;
                        cmd_type <= `GENERAL;
                    end
                end
                `FPRD: begin
                    if(shift_reg[15:0]==configured_station_address_r || shift_reg[15:0]==configured_station_alias_r) begin
                        operation <= `READ;
                        cmd_type <= `GENERAL;
                    end
                end
                `FPWR: begin
                    if(shift_reg[15:0]==configured_station_address_r || shift_reg[15:0]==configured_station_alias_r) begin
                        operation <= `WRITE;
                        cmd_type <= `GENERAL;
                    end
                end
                `FPRW: begin
                    if(shift_reg[15:0]==configured_station_address_r || shift_reg[15:0]==configured_station_alias_r) begin
                        operation <= `WRITE_AFTER_READ;
                        cmd_type <= `GENERAL;
                    end
                end
                `BRD: begin
                    operation <= `READ;
                    cmd_type <= `BROADCAST;
                end
                `BWR: begin
                    operation <= `WRITE;
                    cmd_type <= `BROADCAST;
                end
                `BRW: begin
                    operation <= `WRITE_AFTER_READ;
                    cmd_type <= `BROADCAST;
                end
                `LRD: begin
                    operation <= `READ;
                    cmd_type <= `LOGICAL;
                end
                `LWR: begin
                    operation <= `WRITE;
                    cmd_type <= `LOGICAL;
                end
                `LRW: begin
                    operation <= `WRITE_AFTER_READ;
                    cmd_type <= `LOGICAL;
                end
                `ARMW: begin
                    if(shift_reg[15:0]==0) begin
                        operation <= `READ;
                        cmd_type <= `GENERAL;
                    end
                    else begin
                        operation <= `WRITE;
                        cmd_type <= `GENERAL;
                    end
                end
                `FRMW: begin
                    if(shift_reg[15:0]==configured_station_address_r || shift_reg[15:0]==configured_station_alias_r) begin
                        operation <= `READ;
                        cmd_type <= `GENERAL;
                    end
                    else begin
                        operation <= `WRITE;
                        cmd_type <= `GENERAL;
                    end
                end
                default: begin
                    operation <= `NONE;
                    cmd_type <= `GENERAL;
                end
            endcase
        end
        else if(state != `DATAGRAM_HEADER && state != `DATAGRAM_PAYLOAD) begin
            operation <= `NONE;
            cmd_type <= `GENERAL;
        end
    end    
 
endmodule

